﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketEngine.Gui;
using PocketEngine.Particles;
using PocketEngine.Particles.Types;
using PocketEngine;
using PocketEngine.Lights;
using PocketEngine.Cameras;
using PocketEngine.Rendering;
using PocketEngine.Maps;
using PocketEngine.Maths;
using PocketEngine.Engine;
using PocketEngine.Images;
using FastMath;
using PocketEngine.Prefab;
using PocketEngine.RenderTargets;
namespace PocketBattle
{
    class Program
    {
        public static bool EmitFlames = false;
        public class SP_Demo1 : pSpeechProcessor
        {
            public override void UserSaid(string thesewords, float thissure, System.Speech.Recognition.SpeechRecognizedEventArgs evt)
            {
                //base.UserSaid(thesewords, thissure, evt);
                if (thesewords.Contains("particle"))
                {
                    if(thesewords.Contains("on"))
                    {
                        EmitFlames = true;
                    }
                    if(thesewords.Contains("off"))
                    {
                        EmitFlames = false;
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            pEngine.Misc.Init();
            pEngine.Screen.OpenSimpleWindow();

            pImage tile = new pImage("Content/2D/Tiles/Metal/Metal5.jpg");
            pTile wall1 = new pTile(new pAnimImage(new pImage("Content/2D/Tiles/Metal/Metal3.jpg")));
            
            wall1.CastShadow = true;
            wall1.TakeShadow = false;
            wall1.Blocker = true;

            pMap testMap = new pMap(32, 32, 3, 64,50);
            testMap.Fill(new pTile(new pAnimImage(tile)), 0);
            testMap.Rect(wall1, 4, 4, 2, 6, 0);
            testMap.Rect(wall1,8,6,1,4,0);
            testMap.Rect(wall1, 0, 7, 8, 1, 0);
            testMap.Tiles[2, 7, 0] = new pTile(new pAnimImage(tile));

            pRenderer testRen = new pRenderer();
            pCam vcam = new pCam();
            float ang = 0;

            pLight testLight = new pLight();
            testLight.LightRange = 1000;
            testMap.Add(testLight);


            pLight other = new pLight();

            other.LightRange = 750;
            testMap.Add(other);
            other.DiffuseColor = new pColor(0, 1,1, 1);
            other.SpecularColor = new pColor(1, 1, 0, 1);

            testLight.Position = new pVec3(50, 250, 30);
            other.Position = new pVec3(30, 30, 25);
            testLight.SpecularColor = new pColor(0, 0, 0, 1);
            pImage fire1 = new pImage("Content/2D/Fx/Fire1.png");
            pSquareParticle pt_fire = new pSquareParticle();
            pt_fire.Visual = fire1;
            pt_fire.Decline = 0.962f;
            pt_fire.Drag = 1;
            pt_fire.CastLight = false;

            Random rnd = new Random();
            pRenderTarget pTarget = new pRenderTarget();
            pImage backImg = new pImage(pEngine.Screen.Info.Width, pEngine.Screen.Info.Height,pImageFormat.Rgb);
           
            testMap.InitPhysics();
            
            pEngine.Speech.NewSpeechPattern();
            pEngine.Speech.AddPhrase("particles");
            pEngine.Speech.AddChoice("on", "off");
            pEngine.Speech.FinishSpeechPattern();
            pEngine.AddSpeechProcessor(new SP_Demo1());

            pEngine.Prefab.MakePrefab("Content/RawPrefabs/Base1.png", 96, 96, "Content/Prefabs/Base1.prefab", "Base 1\nMod:Pocket War\nMod Authors:Antony Wells / Nicky Wells\nPrefab Author:Nicky Wells\nCopyright:(c)Team Pocket 2009");
            //pPrefab nickFab = new pPrefab("Content/Prefabs/Base1.prefab");
            //while (true)
            //{
            //}
          //  Environment.Exit(-1);

            pEngine.Speech.StartToListen();
            pWidgetGroup Desktop = new pWidgetGroup(0, 0, pEngine.Screen.Info.Width, pEngine.Screen.Info.Height, null);

            pWindowFrame winMap = new pWindowFrame(40, 25, 300, 120, "Map Stuff", Desktop);
            pButton testBut = new pButton(30, 30, 140, 18, "Load Map", winMap);
            pGuiManager.SetRoot(winMap);

            float px, py;
            float rx, ry, rw, rh;

            rx = 40;
            ry = 40;
            rw = 200;
            rh = 200;
            


            while (true)
            {

                px = pEngine.Input.GetMousePos().X;
                py = pEngine.Input.GetMousePos().Y;

                pEngine.Screen.ClearScreen();

                pGuiManager.Update();
                pVec2 pos = pEngine.Input.GetMousePos();

                Vector3 mouse = new Vector3(pos.X, pos.Y,25);

                other.Position = new pVec3(pos.X, pos.Y, 50);

                for (int i = 0; i < 4; i++)
                {
                    Vector3 vel = new Vector3(-2048 + (float)rnd.NextDouble() * 4096, -2 + (float)rnd.NextDouble() * 4, 2 + (float)rnd.NextDouble() * 2);
                    int type = rnd.Next(1, 3);
                    pColor col = new pColor((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble(), 1);
                    // mouse = new Vector3(300, 300, 20);
                    pt_fire.Simulated = true;
                    if (EmitFlames)
                    {
                        pEngine.Particles.Spawn(pt_fire as pParticle, col, mouse, vel, 16, 1.0f);
                    }
                }
                pEngine.Physics.Update(0.4f);
                pEngine.Particles.Update(testMap);
                

                //vcam.ViewRotation = pos.X;
                //  vcam.ViewScale = 0.5f + pos.Y * 0.01f;
                testRen.UpdateShadowMaps(testMap, vcam);

                //pTarget.Bind();
               // pTarget.AttachImage(backImg);
                testRen.DrawMap(testMap, vcam);
                //pTarget.Unbind();
                
                //pEngine.Pen.Image(20, 20, 350, 350, backImg, pColor.White);

                 pEngine.Particles.Draw(backImg);

                 pGuiManager.DrawFast();

                pEngine.Screen.SwapScreen();
                pEngine.Garbage.Purge();
            }

        }
    }
}
